package com.hyts.rpclet.core;

import com.hyts.rpclet.context.RpcletContext;
import com.hyts.rpclet.execute.RpcletMethodInvoker;
import com.hyts.rpclet.serialize.RpcletSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.rpclet.core
 * author:Libo/Alex
 * create-date:2019-07-09 21:40
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
@Component
@Data
public class RpcletServerDispatcher {


    @Autowired
    private RpcletContext rpcletContext;


    @Autowired
    private RpcletSerializer rpcletSerializer;

    
    /**
     * 操作服务调用执行操作
     * @param invoker
     * @param inputBuf
     * @param outputBuf
     */
    public void invoke(RpcletMethodInvoker invoker, InputStream inputBuf, OutputStream outputBuf){
       Object requestParam = rpcletSerializer.decode(inputBuf,invoker.getArgumentTypeList());
       Object resultParam = invoker.call(requestParam);
       rpcletSerializer.encode(outputBuf,resultParam);
    }

    /**
     * 构造器
     * @param rpcletContext
     */
    public RpcletServerDispatcher(RpcletContext rpcletContext) {
        this.rpcletContext = rpcletContext;
    }
}
